home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / pgp23src.zip / PASSWD.C < prev    next >
C/C++ Source or Header  |  1993-06-11  |  2KB  |  80 lines

  1. /*    passwd.c - Password reading/hashing routines
  2.     (c) 1989 Philip Zimmermann.  All rights reserved.
  3.     Implemented in Microsoft C.
  4.     Routines for getting a pass phrase from the user's console.
  5. */
  6.  
  7. #include    <stdio.h>    /* for fprintf() */
  8. #include    <ctype.h>    /* for isdigit(), toupper(), etc. */
  9. #include    <string.h>    /* for strlen() */
  10.  
  11. #include    "random.h"    /* for getstring() */
  12. #include    "md5.h"
  13. #include    "language.h"
  14. #include    "pgp.h"
  15.  
  16. #define MAXKEYLEN 254    /* max byte length of pass phrase */
  17.  
  18. boolean showpass = FALSE;
  19.  
  20. /*
  21. **    hashpass - Hash pass phrase down to 128 bits (16 bytes).
  22. **  keylen must be less than 1024.
  23. **    Use the MD5 algorithm.
  24. */
  25. void hashpass (char *keystring, int keylen, byte *hash)
  26. {
  27.     MD5_CTX    mdContext;
  28.  
  29.     /* Calculate the hash */
  30.     MD5Init(&mdContext);
  31.     MD5Update(&mdContext, (unsigned char *) keystring, keylen);
  32.     MD5Final(hash, &mdContext);
  33. }    /* hashpass */
  34.  
  35.  
  36. /*
  37. **    GetHashedPassPhrase - get pass phrase from user, hashes it to an IDEA key.
  38.     Parameters:
  39.         returns char *keystring as the pass phrase itself
  40.         return char *hash as the 16-byte hash of the pass phrase
  41.                 using MD5.
  42.         byte noecho:  
  43.             0=ask once, echo. 
  44.             1=ask once, no echo. 
  45.             2=ask twice, no echo.
  46.     Return 0 if no characters are input, else return 1.
  47.     If we return 0, the hashed key will not be useful.
  48. */
  49. int GetHashedPassPhrase(char *hash, boolean noecho)
  50. {    char keystr1[MAXKEYLEN+2], keystr2[MAXKEYLEN+2];
  51.     int len;
  52.  
  53.     if (showpass)
  54.         noecho = 0;
  55.     while (TRUE) {
  56.         fprintf(pgpout,PSTR("\nEnter pass phrase: "));
  57.         getstring(keystr1,MAXKEYLEN-1,!noecho);
  58.         if (noecho<2)    /* no need to ask again if user can see it */
  59.             break;
  60.         fprintf(pgpout,PSTR("\nEnter same pass phrase again: "));
  61.         getstring(keystr2,MAXKEYLEN-1,!noecho);
  62.         if (strcmp(keystr1,keystr2)==0)
  63.             break;
  64.         fprintf(pgpout,PSTR("\n\007Error: Pass phrases were different.  Try again."));
  65.         memset(keystr2, 0, sizeof(keystr2));
  66.     }
  67.     if (noecho && (filter_mode || quietmode))
  68.         putc('\n', pgpout);
  69.  
  70.     len = strlen(keystr1);
  71.     if (len == 0)
  72.         return 0;
  73.     /* We assume ASCII pass phrases, with no charset conversions. */
  74.     /* This will have to change for EBCDIC */
  75.     hashpass (keystr1, strlen(keystr1), (byte *) hash);
  76.     memset(keystr1, 0, sizeof(keystr1));
  77.     return 1;
  78. }    /* GetHashedPassPhrase */
  79.  
  80.